//获取应用实例
let app = getApp();
let httptool=require("../../utils/zry.httptool.js");
Page({
  data:{
    shareLocal:'本市擂盟',
    tabNum:'',
    CircleId:'',//擂盟id
    Content:'',//转发输入的内容
    selectCircle:true,
    selectArea:false,
    radioCheckVal:'',
    selfcircleList:[],//自建盟数据
    params:{},//转发帖子的参数
    forumParam:{},//获取帖子内容的参数
    fourumId:'',//帖子id
    forumData:{},//帖子数据
    msgprop:'',
    Titile:'',//title输入框内容
    evaContent:'',//文本域描述内容 
    sendaddr:'', //编辑时发布的擂盟
    resendArea:true,//重新发帖时选择擂盟的展示 
    editArea:true,//编辑时名字和内容
    etype:0,//跳转过来的类型
    forwardCont:''//编辑时显示的转发内容
  },
  areaContent:function(e){
    this.setData({Content: e.detail.value});
  },
  postTitle:function(e){
    let Titile=e.detail.value;
    this.setData({Titile:Titile});
  },
  charChange:function(e){//文本域值的获取
    this.setData({evaContent:e.detail.value});
    console.log(e.detail.value);
  },
  clearinput:function(e){
    let that=this; 
    that.setData({Titile:''});
  },
  clearCont:function(e){//清除输入的内容
    let that=this; 
    that.setData({evaContent:''});
  },
  tabClick:function(e){//发布擂盟选择
      let that=this;
      let num=e.currentTarget.dataset.num;
      that.setData({ tabNum: num, CircleId: 0, radioCheckVal: ''});
      if(num=="1"){
        that.setData({CircleId:1});
      }
      if(num=="2"){
        that.setData({CircleId:app.getStorage('cityId'),selectCircle:true,selectArea:true});
      }
      if(num=="3"){
          let selectCircle = this.data.selectCircle;
          if(selectCircle == true){
              this.setData({
                selectArea:true,
                selectCircle:false,
            });
          }else{
            this.setData({
              selectArea:false,
              selectCircle:true,
            });
          }
      }else{
         this.setData({
            selectArea:false,
            selectCircle:true,
        });
      }
  },
  radioCheckedChange: function (e) {
    this.setData({
      radioCheckVal: e.detail.value,
      CircleId:e.detail.value
    })
  },
  onLoad:function(options){
    let that=this;
    let etype=options.etype;
    let fourumId=options.fourumId;
    let  referenceId=options.reference;
    if(etype==1){
        wx.setNavigationBarTitle({
          title: '重新编辑'
        })
     that.setData({etype:etype,editArea:false,resendArea:false});
    }
    let CircleName=app.getStorage('CircleName');
    that.setData({shareLocal:CircleName,fourumId:fourumId,referenceId:referenceId});
    that.getForumDetail();
    that.getSelfCircle();
  },
  getForumDetail:function(e){//获取帖子详情带转发内容
      let forumParam={},that=this;
      let selfcircleList=that.data.selfcircleList;
      forumParam.ID=that.data.fourumId;
      forumParam.UserId=app.getStorage('customerId');
      that.setData({forumParam:forumParam});
      httptool.ajaxPost("api/Topic/GetTopicDetail",forumParam,function(res){
        if(res.suc){
          if(res.data!=""&& res.data!=null && res.data!=undefined){
              let data=res.data;
              if (data.Reference>0)
                data.Titile = data.RefTitile;
              that.setData({ forumData: data, Titile: data.Titile, evaContent: data.Content });
              if(data.TopicType==1){
                that.setData({sendaddr:'全国擂盟'});
              }else if(data.TopicType==2){
                that.setData({sendaddr:that.data.shareLocal});
              }else if(data.TopicType==3){
                if(selfcircleList.length==0){
                  that.setData({sendaddr:'我的萌盟'});
                }else if(selfcircleList.length>0){
                  for(let i=0;i<selfcircleList.length;i++){
                    if(that.data.forumData.ID==selfcircleList[i].ID){
                        that.setData({sendaddr:selfcircleList[i].Name});
                    }else{
                      that.setData({sendaddr:'我的萌盟'});
                    }
                  }
                }
              }
          }
        }else{
           app.showNotify(that, res.msg);
        }
      });
  }, 
  getSelfCircle:function(e){//获取自建擂盟的数据
     let that=this;
     let customerId = app.getStorage('customerId');
     httptool.ajaxPost("api/Circle/GetMyCircle", { UserId: customerId},function(res){
        if(res.suc){
          if(res.data!=""&& res.data!=null && res.data!=undefined){
              let data=res.data;
              if(data.DataList.length>0){
                that.setData({selfcircleList:data.DataList});
              }
          }
        }else{
           app.showNotify(that, res.msg);
        }

      });
  },
  forwardForum:function(e){//转发帖子
    let that=this;
    if(that.data.etype==1){
        that.rEdit();
    }else{
        that.forwardTopic();
    }
  },
  forwardTopic: function(e){//转发
    let that = this;
    if (app.trim(that.data.Content)=="")
    {
      app.showNotify(that, "请输入转发内容");
      return false;
    }
    let params={};
    params.Content=that.data.Content;
    params.CircleId=that.data.CircleId;
    params.CreateBy=app.getStorage('customerId');
    params.Reference=that.data.fourumId;
    params.TopicType=that.data.tabNum;
    that.setData({params:params});
    httptool.ajaxPost("api/Topic/AddTopic", params, function(res){
      if(res.suc){
        wx.navigateBack(); 
      }else{
        app.showNotify(that, res.msg);
      }
    });
  },
  rEdit:function(e){//修改编辑有转发内容的的帖子
    let that=this;
    let editForum={};
    editForum.UserId = app.getStorage('customerId');
    editForum.Content = that.data.evaContent;
    editForum.Titile = that.data.Titile;
    editForum.ID = that.data.fourumId;
    editForum.ImgList = [];
    httptool.ajaxPost("api/Topic/UpdateTopic", editForum, function (res) {
      if (res.suc) {
        wx.navigateBack(); 
      } else {
        app.showNotify(that, res.msg);
      }
      wx.hideLoading();
    }, function () { wx.hideLoading(); });
  }
})